import CryptoJS from 'crypto-js'
// 加密
export default {
  encrypt (word, keyStr, iv) {
    const keys = CryptoJS.enc.Utf8.parse(keyStr) // '82LIb3DQE9ByIdz9'
    const ivs = CryptoJS.enc.Utf8.parse(iv) // iv '0102030405060708'
    const srcs = CryptoJS.enc.Utf8.parse(word)
    const encrypted = CryptoJS.AES.encrypt(srcs, keys, {
      iv: ivs,
      mode: CryptoJS.mode.CBC,
      padding: CryptoJS.pad.Pkcs7
    })
    console.log('base64:' + encrypted)
    // var encryptedStr = encrypted.ciphertext.toString()
    // console.log('16进制:' + encryptedStr)
    // return encrypted.ciphertext.toString().toUpperCase();  //16进制
    return encrypted.toString() // base64
  },
  // }

  // 解密
  decrypt64 (word, keyStr, iv) {
    // ********************base64:***********************
    const key = CryptoJS.enc.Utf8.parse(keyStr)
    const ivs = CryptoJS.enc.Utf8.parse(iv)
    const decrypt = CryptoJS.AES.decrypt(word, key, {
      iv: ivs,
      mode: CryptoJS.mode.CBC,
      padding: CryptoJS.pad.Pkcs7
    })
    const decryptedStr = decrypt.toString(CryptoJS.enc.Utf8)
    return decryptedStr.toString()
  },

  // 解密
  decrypt (word, keyStr, iv) {
    // ********************解密16进制***********************
    const key = CryptoJS.enc.Utf8.parse(keyStr) // '82LIb3DQE9ByIdz9'
    const ivs = CryptoJS.enc.Utf8.parse(iv) // '0102030405060708'
    const encryptedHexStr = CryptoJS.enc.Hex.parse(word)
    const srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr)
    const decrypt = CryptoJS.AES.decrypt(srcs, key, {
      iv: ivs,
      mode: CryptoJS.mode.CBC,
      padding: CryptoJS.pad.Pkcs7
    })
    const decryptedStr = decrypt.toString(CryptoJS.enc.Utf8)
    return decryptedStr.toString()
  }

}
